package com.jlu.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jlu.yygh.DictFeignClient;
import com.jlu.yygh.model.hosp.Hospital;
import com.jlu.yygh.repository.HospitalRepository;
import com.jlu.yygh.service.HospitalService;
import com.jlu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

  @Autowired
  private HospitalRepository hospitalRepository;

  @Autowired
  private DictFeignClient dictFeignClient;

  @Override
  public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
    //0为第一页
    Pageable pageable = PageRequest.of(page-1, limit);

    Hospital hospital = new Hospital();
    BeanUtils.copyProperties(hospitalQueryVo, hospital);

//创建匹配器，即如何使用查询条件
    ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
            .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

    //创建实例
    Example<Hospital> example = Example.of(hospital, matcher);
    Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

    pages.getContent().stream().forEach(item -> {
      this.packHospital(item);
    });

    return pages;
  }

  @Override
  public Hospital getByHoscode(String hoscode) {
    return hospitalRepository.getHospitalByHoscode(hoscode);
  }

  @Override
  public void save(Map<String, Object> paramMap) {
//    把集合转化成对象
    String mapString = JSONObject.toJSONString(paramMap);
    Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);

//    判断是否存在数据
    hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
//    如果不存在，则添加
    if(null != hospital) {
      hospital.setStatus(0);
      hospital.setCreateTime(new Date());
      hospital.setUpdateTime(new Date());
      hospital.setIsDeleted(0);
      hospitalRepository.save(hospital);
    } else {
//      如果存在，则修改
//      0：未上线 1：已上线
      hospital.setStatus(hospital.getStatus());
      hospital.setCreateTime(hospital.getCreateTime());
      hospital.setUpdateTime(new Date());
      hospital.setIsDeleted(0);
      hospitalRepository.save(hospital);
    }
  }


  /**
   * 封装数据
   * @param hospital
   * @return
   */
  private Hospital packHospital(Hospital hospital) {
    String hostypeString = dictFeignClient.getName("Hostype",hospital.getHostype());
    String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
    String cityString = dictFeignClient.getName(hospital.getCityCode());
    String districtString = dictFeignClient.getName(hospital.getDistrictCode());

    hospital.getParam().put("hostypeString", hostypeString);
    hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());
    return hospital;
  }

  @Override
  public void updateStatus(String id, Integer status) {
    if(status.intValue() == 0 || status.intValue() == 1) {

//      先根据id查询医院，再设置新的值
      Hospital hospital = hospitalRepository.findById(id).get();
      hospital.setStatus(status);
      hospital.setUpdateTime(new Date());
      hospitalRepository.save(hospital);
    }
  }

  @Override
  public Map<String, Object> show(String id) {
    Map<String, Object> result = new HashMap<>();

    Hospital hospital = this.packHospital(hospitalRepository.findById(id).get());
    result.put("hospital", hospital);

//单独处理更直观
    result.put("bookingRule", hospital.getBookingRule());
//不需要重复返回
    hospital.setBookingRule(null);
    return result;
  }

  @Override
  public String getName(String hoscode) {

    Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
    if (hospital != null){
      return hospital.getHosname();
    }
    return null;
  }

  @Override
  public List<Hospital> findByHosname(String hosname) {

    List<Hospital> hospital = hospitalRepository.findHospitalByHosnameLike(hosname);
    return hospital;
  }

  @Override
  public Map<String, Object> item(String hoscode) {
    Map<String, Object> result = new HashMap<>();
    //医院详情
    Hospital hospital = this.packHospital(this.getByHoscode(hoscode));
    result.put("hospital", hospital);
    //预约规则
    result.put("bookingRule", hospital.getBookingRule());
    //不需要重复返回
    hospital.setBookingRule(null);
    return result;
  }
}
